﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Read.API.Application.Command.UserInfoCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Request;
using RBAC_Uthorization.Read.API.DTO.Response;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace RBAC_Uthorization.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class LoginController : ControllerBase
    {
 
        private readonly IMediator _mediator;
        private readonly ILogger<LoginController> _logger;
        private readonly IConfiguration _configuration;

        public LoginController(IMediator mediator, ILogger<LoginController> logger)
        {
            _mediator = mediator;
            _logger = logger;
    
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<string>> Login(LoginUserPwddto dto)
        {
            try
            {
                var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
                return await  _mediator.Send(new userLogincommand { IpAddress = ipAddress,sys_id=dto.SystemCode,password = dto.password, username = dto.username });
                 
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "登录时发生错误");
                return new ApiResult<string>() { Code=ResultCode.Forbidden,Message= "登录时发生错误" };
            }
        }
        //var sub = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
        //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
        //var jti = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value;//获取系统id
        //var uri = HttpContext.User.FindFirst(ClaimTypes.Uri)?.Value;//获取头像
        //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
        //var claims = HttpContext.User.Claims;


    }
}
